﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TRACv2.Persistence;
using AutoMapper;
using TRACv2.Domain.DTO;
using System.Diagnostics.CodeAnalysis;

namespace TRACv2.Persistence.Test
{
    // Author: Ryan Redburn
    // Project: TRACv2
    // Date: 3/21/2014

    [TestClass]
    [ExcludeFromCodeCoverage]
    public class EmployeeRepositoryTest
    {
        // GetAll

        [TestMethod]
        public void GetAll_Call_ValidReturn()
        {
            // Arrange
            var empRepo = new EmployeeRepository();
            Mapper.CreateMap<Employee, EmployeeDTO>();

            // Act
            var employees = empRepo.GetAll();

            // Assert
            Assert.IsNotNull(employees);
        }

        // GetByLastName

        [TestMethod]
        public void GetByLastName_ValidLastName_ValidReturn()
        {
            // Arrange
            var empRepo = new EmployeeRepository();
            Mapper.CreateMap<Employee, EmployeeDTO>();
            var lastName = "Smith"; // Exists in database

            // Act
            var employee = empRepo.GetByLastName(lastName);

            // Assert
            Assert.IsNotNull(employee);
        }

        [TestMethod]
        public void GetByLastName_NullLastNameValue_ArgumentNullException()
        {
            // Arrange
            var empRepo = new EmployeeRepository();
            string lastName = null;
            var assert = false;

            // Act
            try
            {
                var employee = empRepo.GetByLastName(lastName);
            }
            catch (ArgumentNullException)
            {
                assert = true;
            }
            finally
            {
                // Assert
                Assert.IsTrue(assert);
            }
        }

        [TestMethod]
        public void GetByLastName_EmptyLastNameValue_ArgumentNullException()
        {
            // Arrange
            var empRepo = new EmployeeRepository();
            string lastName = "";
            var assert = false;

            // Act
            try
            {
                var employee = empRepo.GetByLastName(lastName);
            }
            catch (ArgumentNullException)
            {
                assert = true;
            }
            finally
            {
                // Assert
                Assert.IsTrue(assert);
            }
        }

        // GetById

        [TestMethod]
        public void GetById_ValidId_ValidReturn()
        {
            // Arrange
            var empRepo = new EmployeeRepository();
            Mapper.CreateMap<Employee, EmployeeDTO>();
            int id = 3; // Exists in database

            // Act
            var employee = empRepo.GetById(id);

            // Assert
            Assert.IsNotNull(employee);
        }

        [TestMethod]
        public void GetById_IdValueLessThanOne_ArgumentOutOfRangeException()
        {
            // Arrange
            var empRepo = new EmployeeRepository();
            int id = 0;
            var assert = false;

            // Act
            try
            {
                var employee = empRepo.GetById(id);
            }
            catch (ArgumentOutOfRangeException)
            {
                assert = true;
            }
            finally
            {
                // Assert
                Assert.IsTrue(assert);
            }
        }

        [TestMethod]
        public void GetById_InvalidId_InvalidOperationException()
        {
            // Arrange
            var empRepo = new EmployeeRepository();
            int id = 1001; // Does not exist in database
            var assert = false;

            // Act
            try
            {
                var employee = empRepo.GetById(id);
            }
            catch (InvalidOperationException)
            {
                assert = true;
            }
            finally
            {
                Assert.IsTrue(assert);
            }
        }

        // GetByCode

        [TestMethod]
        public void GetByCode_ValidCode_ValidReturn()
        {
            // Arrange
            var empRepo = new EmployeeRepository();
            string code = "jsmith";
            Mapper.CreateMap<Employee, EmployeeDTO>();

            // Act
            var employee = empRepo.GetByCode(code);

            // Assert
            Assert.IsNotNull(employee);
        }

        [TestMethod]
        public void GetByCode_NullCodeValue_ArgumnetNullException()
        {
            // Arrange
            var empRepo = new EmployeeRepository();
            string code = null;
            var assert = false;

            // Act
            try
            {
                var employee = empRepo.GetByCode(code);
            }
            catch (ArgumentNullException)
            {
                assert = true;
            }
            finally
            {
                // Assert
                Assert.IsTrue(assert);
            }
        }

        [TestMethod]
        public void GetByCode_EmptyCodeValue_ArgumnetNullException()
        {
            // Arrange
            var empRepo = new EmployeeRepository();
            string code = "";
            var assert = false;

            // Act
            try
            {
                var employee = empRepo.GetByCode(code);
            }
            catch (ArgumentNullException)
            {
                assert = true;
            }
            finally
            {
                // Assert
                Assert.IsTrue(assert);
            }
        }

        [TestMethod]
        public void GetByCode_InvalidCode_InvalidoperationException()
        {
            // Arrange
            var empRepo = new EmployeeRepository();
            var code = "hmacoy"; // Does not exist in database
            var assert = false;

            // Act
            try
            {
                var employee = empRepo.GetByCode(code);
            }
            catch (InvalidOperationException)
            {
                assert = true;
            }
            finally
            {
                // Assert
                Assert.IsTrue(assert);
            }
        }
    }
}
